package edu.sjsu.cs286.teodoro.cipresso.bluetooth;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import edu.sjsu.cs286.teodoro.cipresso.playfield.BlueTankGameCanvas;
import edu.sjsu.cs286.teodoro.cipresso.playfield.BlueTankMIDlet;
import edu.sjsu.cs286.teodoro.cipresso.sprites.BlueTankImageHolder;
import edu.sjsu.cs286.teodoro.cipresso.sprites.BlueTankVehicleSprite;

/**
 * This class implements a graphical screen that displays game status messages
 * to the user while a game between peers is being setup for play.
 * 
 * @author Teodoro Cipresso
 */
public class BlueTankGameStatusScreen extends Canvas implements CommandListener {

	private BlueTankMIDlet midlet;

	private Vector status;

	private Image titleImage;

	private boolean showTankSelections = false;

	private Command hostCmd = new Command("Host Game", Command.ITEM, 1);

	private Command joinCmd = new Command("Join Game", Command.ITEM, 1);

	private Command exitCmd = new Command("Exit", Command.EXIT, 10);

	/**
	 * Default constructor
	 */
	public BlueTankGameStatusScreen(BlueTankMIDlet parent) {
		super();
		//
		midlet = parent;
		status = new Vector();
		//
		addCommand(exitCmd);
		setCommandListener(this);
		//
	} // BlueTankGameStatusScreen()

	/**
	 * Paint the canvas based on the information we want to show the user
	 */
	protected void paint(Graphics gfx) {
		// get dimensions
		int width = getWidth();
		int height = getHeight();
		// draw camouflage background
		Image bg = BlueTankImageHolder.getCamouflage(width, height);
		gfx.drawImage(bg, 0, 0, 0);
		// write out status strings...
		Font font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN,
				Font.SIZE_SMALL);
		gfx.setFont(font);
		gfx.setColor(0xFFFFFF);
		//
		if (!showTankSelections) {
			paintStatusMessages(gfx, font, width, height);
		} else {
			paintTankSelections(gfx, font, width, height);
		}
	} // paint()

	/**
	 * This method paints a version of the status screen that shows which the
	 * tanks the players selected before starting the game
	 */
	private void paintTankSelections(Graphics gfx, Font font, int width,
			int height) {
		Image image = null;
		BlueTankGameCanvas btCanvas = midlet.getGameContainer().getBtCanvas();
		//
		try {
			titleImage = Image.createImage("/preparetitle.gif");
			gfx.drawImage(titleImage, 5, 5, Graphics.TOP | Graphics.LEFT);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//
		gfx.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
				Font.SIZE_MEDIUM));
		// paint self ownerTank, if selected
		BlueTankVehicleSprite tank = btCanvas.getSelfTank();
		if (tank != null) {
			image = BlueTankImageHolder.getTankSelectImageByName(tank
					.getTankName());
			gfx.setColor(BlueTankImageHolder.getTankRgbByName(tank.getTankName()));
		}
		if (image != null) {
			//
			Double x = new Double(width * .33);
			Double y = new Double(height * .45);
			//
			gfx.drawString("YOU", x.intValue(), y.intValue(), Graphics.TOP
					| Graphics.HCENTER);
			//
			y = new Double(y.intValue()
					+ (image.getHeight() + font.getHeight()));
			gfx.drawImage(image, x.intValue(), y.intValue(), Graphics.TOP
					| Graphics.HCENTER);
			//
			image = null;
		}
		// paint peer ownerTank, if selected
		tank = btCanvas.getPeerTank();
		if (tank != null) {
			image = BlueTankImageHolder.getTankSelectImageByName(tank
					.getTankName());
			gfx.setColor(BlueTankImageHolder.getTankRgbByName(tank.getTankName()));			
		}
		if (image != null) {
			//
			Double x = new Double(width * .66);
			Double y = new Double(height * .45);
			//
			gfx.drawString("OPPONENT", x.intValue(), y.intValue(), Graphics.TOP
					| Graphics.HCENTER);
			//
			y = new Double(y.intValue()
					+ (image.getHeight() + font.getHeight()));
			gfx.drawImage(image, x.intValue(), y.intValue(), Graphics.TOP
					| Graphics.HCENTER);
			//
			image = null;
		} //
		gfx.setColor(0x000000);
	} // paintTankSelections()

	/**
	 * Paint a version of the screen that shows successive status messages.
	 */
	private void paintStatusMessages(Graphics gfx, Font font, int weight,
			int height) {
		//
		try {
			titleImage = Image.createImage("/gametitleimage.gif");
			gfx.drawImage(titleImage, 5, 5, Graphics.TOP | Graphics.LEFT);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//
		Enumeration e = status.elements();
		int count = 1, x = 5, y = (height / 4) + 5;
		while (e.hasMoreElements()) {
			gfx.drawString(e.nextElement().toString(), x, y, Graphics.TOP
					| Graphics.LEFT);
			count++;
			y += gfx.getFont().getHeight() * 1.2;
		}
	} // paintStatusMessages()

	/**
	 * Set whether tank selections screen should be painted
	 */
	public void setPaintTankSelections(boolean showTank) {
		this.showTankSelections = showTank;
		repaint();
		serviceRepaints();
	} // setPaintTankSelections()

	/**
	 * Force the canvas to repaint
	 */
	public void forceRepaint() {
		repaint();
		serviceRepaints();
	} //

	/**
	 * Add a message to be painted
	 */
	public void addStatusMessage(String msg) {
		status.addElement(msg);
		repaint();
		serviceRepaints();
	} // addStatusMessage()

	/**
	 * Clear list of messages to be painted
	 */
	public void clearStatusMessages() {
		status.removeAllElements();
		repaint();
		serviceRepaints();
	} // addStatusMessage()

	/**
	 * Add connectivity softkeys to the canvas
	 */
	public void addConnectSoftKeys() {
		addCommand(hostCmd);
		addCommand(joinCmd);
		setCommandListener(this);
	} // setConnectSoftKeys()

	/**
	 * Clear connectivity softkeys from the canvas
	 */
	public void removeConnectSoftKeys() {
		removeCommand(hostCmd);
		removeCommand(joinCmd);
		setCommandListener(this);
	} // removeConnectSoftKeys()

	/**
	 * Handle softkey actions
	 */
	public void commandAction(Command arg0, Displayable arg1) {
		String commStr = arg0.getLabel();
		if (commStr.equals(hostCmd.getLabel())) {
			midlet.getGameContainer().getBtManager().doBtServerInit();
		} else if (commStr.equals(joinCmd.getLabel())) {
			midlet.getGameContainer().getBtManager().doBtClientInit();
		} else if (commStr.equals(exitCmd.getLabel())) {
			midlet.destroyApp(true);
		}
	} // commandAction()

} // BlueTankGameStatusScreen
